package com.aeks.divide;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FileDivide {
	//获得“摘要”，“关键字”字样，获得title，brief， key，可能获得text。
	//成功返回0，获得text返回2，否则返回1
	public static int getTitleBriefkey(String s, String servletPath, int bi, int ki, String saveName) {
		boolean isSuccess = true;
		String title = null;
		String brief = null;
		String key = null;
		String text = null;
		
		if(s == null || s.length() <= 0 || s.length() < (ki+1) ) {
			isSuccess = false;
			return 0;
		} else {
			title = s.substring(0, bi-1);
			brief = s.substring(bi + 2, ki-1);
			
			if(s.length() >  (ki + 50)) {//如果”关键字“字样后面的字超过50个,超过的则为正文
				key = s.substring(ki + 3, ki + 50);
				text = s.substring(ki + 50);
			} else {
				key = s.substring(ki + 3);
			}
			
			System.out.println(title);
			System.out.println(brief);
			System.out.println(key);
			if(text != null) {
				System.out.println(text);
			}
			
			if(title != null && !title.equals("")) {
				if(!FileDivide.inputTitleFile(title, servletPath, saveName)) {
					isSuccess = false;
					return 0;
				}
			} else {
				isSuccess = false;
				return 0;
			}
			
			if(brief != null && !brief.equals("")) {
				if(!FileDivide.inputBriefFile(brief, servletPath, saveName)) {
					isSuccess = false;
					return 0;
				}
			} else {
				isSuccess = false;
				return 0;
			}
			
			if(key != null && !key.equals("")) {
				if(!FileDivide.inputKeyFile(key, servletPath, saveName)) {
					isSuccess = false;
					return 0;
				}
			} else {
				isSuccess = false;
				return 0;
			}
			
			if(text != null && !text.equals("")) {
				if(!FileDivide.inputTextFile(text, servletPath, saveName)) {
					isSuccess = false;
					return 0;
				}
			} 
			
			if(isSuccess) {
				if(text != null) {
					return 2;
				} else {
					return 1;
				}
			} else {
				return 0;
			}
		}
	}
	
	public static boolean inputTitleFile(String s, String servletPath, String saveName) {
		boolean isSuccess = true;
		String titlePart = servletPath + "\\" + saveName + "titlePart.txt";
		File tpf = new File(titlePart);
		/*if(!tpf.exists()) {
			tpf.mkdirs();
		}*/
		
		FileWriter fr = null;
		BufferedWriter bw = null;
		try {
			fr = new FileWriter(tpf, true);
			bw = new BufferedWriter(fr);
			bw.write(s);
		} catch (IOException e) {
			e.printStackTrace();
			isSuccess = false;
		} finally {
			try {
				bw.close();
				fr.close();
			} catch (IOException e) {
				e.printStackTrace();
				isSuccess = false;
			}
		}
		
		return isSuccess;
	}
	
	public static boolean inputBriefFile(String s, String servletPath, String saveName) {
		boolean isSuccess = true;
		String briefPart = servletPath + "\\" + saveName + "briefPart.txt";
		File bpf = new File(briefPart);
		
		/*if(!bpf.exists()) {
			bpf.mkdirs();
		}*/
		
		FileWriter fr = null;
		BufferedWriter bw = null;
		try {
			fr = new FileWriter(bpf, true);
			bw = new BufferedWriter(fr);
			bw.write(s);
		} catch (IOException e) {
			e.printStackTrace();
			isSuccess = false;
		} finally {
			try {
				bw.close();
				fr.close();
			} catch (IOException e) {
				e.printStackTrace();
				isSuccess = false;
			}
		}
		return isSuccess;
	}
	
	public static boolean inputKeyFile(String s, String servletPath, String saveName) {
		boolean isSuccess = true;
		String keyPart = servletPath + "\\" + saveName + "keyPart.txt";
		File kpf = new File(keyPart);
		/*if(!kpf.exists()) {
			kpf.mkdirs();
		}*/
		
		FileWriter fr = null;
		BufferedWriter bw = null;
		try {
			fr = new FileWriter(kpf, true);
			bw = new BufferedWriter(fr);
			bw.write(s);
		} catch (IOException e) {
			e.printStackTrace();
			isSuccess = false;
		} finally {
			try {
				bw.close();
				fr.close();
			} catch (IOException e) {
				e.printStackTrace();
				isSuccess = false;
			}
		}
		return isSuccess;
	}
	
	public static boolean inputTextFile(String s, String servletPath, String saveName) {
		boolean isSuccess = true;
		String textPart = servletPath + "\\" + saveName + "textPart.txt";
		File ttpf = new File(textPart);
		/*if(!ttpf.exists()) {
			ttpf.mkdirs();
		}*/
		
		FileWriter fr = null;
		BufferedWriter bw = null;
		try {
			fr = new FileWriter(ttpf, true);
			bw = new BufferedWriter(fr);
			bw.write(s);
		} catch (IOException e) {
			e.printStackTrace();
			isSuccess = false;
		} finally {
			try {
				bw.close();
				fr.close();
			} catch (IOException e) {
				e.printStackTrace();
				isSuccess = false;
			}
		}
		return isSuccess;
	}
	
	public static boolean inputStopFile(List<String> slist, String path, String saveName) {
		boolean isSuccess = true;
		String stopPart = path + "\\" + saveName + "usedStop.txt";
		System.out.println(stopPart);
		File ttpf = new File(stopPart);
		/*if(!ttpf.exists()) {
			ttpf.mkdirs();
		}*/
		
		FileWriter fr = null;
		BufferedWriter bw = null;
		try {
			fr = new FileWriter(ttpf, true);
			bw = new BufferedWriter(fr);
			for(int i=0; i<slist.size(); i++) {
				String s = slist.get(i);
				System.out.println(s);
				bw.write(s + "/");
			}
		} catch (IOException e) {
			e.printStackTrace();
			isSuccess = false;
		} finally {
			try {
				bw.close();
				fr.close();
			} catch (IOException e) {
				e.printStackTrace();
				isSuccess = false;
			}
		}
		return isSuccess;
	}
	
	//获得“摘要”字样，获得title，brief
	//不成功返回true
	public static boolean getTitleBrief(String s, String servletPath, int bi, String saveName) {
		String title = null;
		String brief = null;
		
		if(s == null || s.length() <= 0 || s.length() < (bi+1) ) {
			return false;
		} else {
			title = s.substring(0, bi-1);
			brief = s.substring(bi + 2);
			
			System.out.println(title);
			System.out.println(brief);
			if(title != null && !title.equals("")) {
				if(!FileDivide.inputTitleFile(title, servletPath, saveName)) {
					return false;
				}
			} else {
				return false;
			}
			
			if(brief != null && !brief.equals("")) {
				if(!FileDivide.inputBriefFile(brief, servletPath, saveName)) {
					return false;
				}
			} else {
				return false;
			}
		}
		return true;	
	}
	
	//获得“关键字”字样，获得brief， key，可能获得text。
	//不成功返回0，获得text返回2，否则返回1
	public static int getBriefKey(String s, String servletPath, int ki, String saveName) {
		boolean isSuccess = true;
		String brief = null;
		String key = null;
		String text = null;
		
		if(s == null || s.length() <= 0 || s.length() < (ki + 1)) {
			isSuccess = false;
			return 0;
		} else {
			if(ki != 0) {
				brief = s.substring(0, ki-1);
			} 
			
			if(s.length() >  (ki + 50)) {//如果”关键字“字样后面的字超过50个,超过的则为正文
				key = s.substring(ki + 3, ki + 50);
				text = s.substring(ki + 51);
			} else {
				key = s.substring(ki + 3);
			}
			
			System.out.println(brief);
			System.out.println(key);
			if(text != null) {
				System.out.println(text);
			}
			
			if(brief != null && !brief.equals("")) {
				if(!FileDivide.inputBriefFile(brief, servletPath, saveName)) {
					isSuccess = false;
					return 0;
				}
			} else {
				isSuccess = false;
				return 0;
			}
			
			if(key != null && !key.equals("")) {
				if(!FileDivide.inputKeyFile(key, servletPath, saveName)) {
					isSuccess = false;
					return 0;
				}
			} else {
				isSuccess = false;
				return 0;
			}
			
			if(text != null && !text.equals("")) {
				if(!FileDivide.inputTextFile(text, servletPath, saveName)) {
					isSuccess = false;
					return 0;
				}
			} 
			
			if(isSuccess) {
				if(text != null) {
					return 2;
				} else {
					return 1;
				}
			} else {
				return 0;
			}
		}
	}
	
	//获得brief，成功返回true
	public static boolean getBrief(String s, String servletPath, String saveName) {
		String brief = null;
		if(s == null || s.length() <= 0 ) {
			return false;
		} else {
			brief = s;
			
			System.out.println(brief);
			if(brief != null && !brief.equals("")) {
				if(!FileDivide.inputBriefFile(brief, servletPath, saveName)) {
					return false;
				}
			} else {
				return false;
			}
		}
		return true;	
	}
	
	//获得text，成功返回true
	public static boolean getText(String s, String servletPath, String saveName) {
		String text = null;
		if(s == null || s.length() <= 0 ) {
			return false;
		} else {
			text = s;
			
			System.out.println(text);
			if(text != null && !text.equals("")) {
				if(!FileDivide.inputTextFile(text, servletPath, saveName)) {
					return false;
				}
			} else {
				return false;
			}
		}
		return true;	
	}
	
	public static List<String> deleteStop(String[] result, List<String> slist, String servletPath) {
		List<String> list = new ArrayList<String>();
		String stopname = "stopwords.txt";
		stopname = servletPath + "material\\" + stopname;
		HashMap hm = new HashMap();
		GenerateDictionary genDic = new GenerateDictionary();
		try {
			genDic.GenHashDic(stopname, hm);
			for(int i=0; i<result.length; i++) {
				String s = result[i];
				if(hm.containsKey(s)) {//停用词中含有该词
					System.out.println("stop:" + s);
					slist.add(s);//加入停用词的list
				} else {
					System.out.println("key:" + s);
					list.add(s);
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return list;
	}
}
